package com.abc.robot01.activity

import android.os.Bundle
import android.util.Log
import androidx.activity.compose.setContent
import androidx.activity.viewModels
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.clickable
import androidx.compose.foundation.horizontalScroll
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.ColumnScope
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.RowScope
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.text.BasicText
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.State
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.Color.Companion.White
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontWeight
import com.abc.robot01.R
import com.abc.robot01.retrofit.bean.DesignPackage
import com.abc.robot01.vivemodel.FAXiangQingViewModel
import com.bumptech.glide.integration.compose.ExperimentalGlideComposeApi
import com.bumptech.glide.integration.compose.GlideImage

/**
 * 方案详情
 */
class FAXiangQingActivity : BaseVoiceActivity() {

    private val viewModel: FAXiangQingViewModel by viewModels()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val id = intent.getStringExtra("id")!!
        viewModel.loadProductDetail(id);
        setContent {
            val commodity by viewModel.commodity.collectAsState()
            val scrollState = rememberScrollState()
            MaterialTheme {
                Box {
                    Image(painter = painterResource(id = R.drawable.z1), contentDescription = "")
                    Row(
                        modifier = Modifier.padding(
                            start = 30.px,
                            end = 30.px,
                            top = 30.px
                        )
                    ) {
                        Spacer(modifier = Modifier.width(144.px))
                        Spacer(modifier = Modifier.width(82.px))

                        Column(
                            modifier = Modifier
                                .fillMaxHeight()
                                .width(1400.px)
                        ) {
                            BasicText(
                                text = "详情",
                                style = TextStyle(
                                    fontSize = 30.mysp,
                                    color = Color.Black,
                                    fontWeight = FontWeight.Bold
                                ),
                            )
                            Spacer(modifier = Modifier.height(29.px))
                            MyCard {
                                Column(
                                    Modifier
                                        .padding(
                                            start = 30.px,
                                            end = 30.px,
                                            top = 30.px,
                                            bottom = 0.px
                                        )
                                        .verticalScroll(scrollState)
                                        .fillMaxSize(),
                                    verticalArrangement = Arrangement.spacedBy(30.px)
                                ) {
                                    commodity?.let {Content(it)}
                                }
                            }
                        }
                    }
                    Box(modifier = Modifier.padding(30.px)
                        .align(Alignment.BottomStart)) {
                        BackButton()
                    }
                }
            }
        }
        Log.e(TAG, "onCreate");
    }

    @OptIn(ExperimentalGlideComposeApi::class)
    @Composable
    fun ScrollableImage(url: State<String?>) {
        // Remember scroll states for both horizontal and vertical scrolling
        val horizontalScrollState = rememberScrollState()
        val verticalScrollState = rememberScrollState()

        Box(
            modifier = Modifier
                .size(540.px) // Set the viewport size to 200x200
                .background(Color.LightGray) // Optional background for the frame
                .horizontalScroll(horizontalScrollState) // Enable horizontal scrolling
                .verticalScroll(verticalScrollState) // Enable vertical scrolling
        ) {
            GlideImage(
                model = url.value,
                contentDescription = "",
                modifier = Modifier
                    .clickable {
                    },
            )
        }
    }

    @Composable
    fun SubContent(commodity: DesignPackage){
        Column {
            Row{
                Box(
                    contentAlignment = Alignment.CenterStart,
                    modifier = Modifier.height(84.px)
                        .background(
                            color = MYBLACK_Percent5, // 设置背景颜色
                            shape = RoundedCornerShape(topStart = 20.px) // 设置圆角
                        )
                        .padding(start = 20.px, end = 84.px)
//                        .padding(16.dp) // 内边距，以确保文本不会紧贴边缘
                ) {
                    Text(
                        text = "适用空间",
                        style = TextStyle(
                            color = MYBLACK,
                            fontSize = 24.mysp
                        )
                    )
                }

                Box(
                    contentAlignment = Alignment.CenterStart,
                    modifier = Modifier.size(1104.px,84.px).border(width = 1.px,
                        color = MYBLACK_Percent10)
                        .background(
                            color = White, // 设置背景颜色
                        ).padding(start = 20.px)
                ) {
                    Text(
                        text = ""+commodity.adaptationSpaceValue,
                        style = TextStyle(
                            color = MYBLACK,
                            fontSize = 24.mysp
                        )
                    )
                }
            }
            Row{
                Box(
                    contentAlignment = Alignment.CenterStart,
                    modifier = Modifier.height(84.px)
                        .background(
                            color = MYBLACK_Percent5, // 设置背景颜色
                        )
                        .padding(start = 20.px, end = 84.px)
//                        .padding(16.dp) // 内边距，以确保文本不会紧贴边缘
                ) {
                    Text(
                        text = "方案材料",
                        style = TextStyle(
                            color = MYBLACK,
                            fontSize = 24.mysp
                        )
                    )
                }

                Box(
                    contentAlignment = Alignment.CenterStart,
                    modifier = Modifier.size(1104.px,84.px).border(width = 1.px,
                        color = MYBLACK_Percent10)
                        .background(
                            color = White, // 设置背景颜色
                        ).padding(start = 20.px)
                ) {
                    Text(
                        text = ""+commodity.mainTreeStr,
                        style = TextStyle(
                            color = MYBLACK,
                            fontSize = 24.mysp
                        )
                    )
                }
            }

            Row{
                Box(
                    contentAlignment = Alignment.CenterStart,
                    modifier = Modifier.height(84.px)
                        .background(
                            color = MYBLACK_Percent5, // 设置背景颜色
                        )
                        .padding(start = 20.px, end = 84.px)
//                        .padding(16.dp) // 内边距，以确保文本不会紧贴边缘
                ) {
                    Text(
                        text = "硬装标准",
                        style = TextStyle(
                            color = MYBLACK,
                            fontSize = 24.mysp
                        )
                    )
                }

                Box(
                    contentAlignment = Alignment.CenterStart,
                    modifier = Modifier.size(1104.px,84.px).border(width = 1.px,
                        color = MYBLACK_Percent10)
                        .background(
                            color = White, // 设置背景颜色
                        ).padding(start = 20.px)
                ) {
                    Text(
                        text = ""+commodity.yzPriceRangeValue,
                        style = TextStyle(
                            color = MYBLACK,
                            fontSize = 24.mysp
                        )
                    )
                }
            }
        }
    }

    @OptIn(ExperimentalGlideComposeApi::class)
    @Composable
    fun ColumnScope.Content(commodity: DesignPackage) {
        val images = viewModel.images.collectAsState().value?.split(",")
        val big = viewModel.bigImg.collectAsState()
        val detailImg by viewModel.detailImg.collectAsState()
        Row(horizontalArrangement = Arrangement.spacedBy(20.px)) {
            Column(verticalArrangement = Arrangement.spacedBy(20.px)) {
                images?.forEach { image ->
                    Log.e(TAG, "图片: $image")
                    GlideImage(
                        model = image,
                        contentDescription = "",
                        modifier = Modifier
                            .align(Alignment.CenterHorizontally)
                            .size(316.px, 176.px)
                            .clickable {
                                viewModel.bigImg.value = image
                            },
                    )

                }
            }
            ScrollableImage(big)
            Title()
        }
        Spacer(modifier = Modifier.height(40.px))
        SubContent(commodity)

        //图片
        GlideImage(
            model = viewModel.detailImg.collectAsState().value?:R.drawable.detail,
            contentDescription = "",
            modifier = Modifier
                .align(Alignment.CenterHorizontally)
        )
    }

    @Composable
    fun RowScope.Title() {
        val commodity by viewModel.commodity.collectAsState()
        Column {
            Text(
                maxLines = 2,
                text = commodity?.name ?: "",
                style = TextStyle(
                    color = MYBLACK, fontSize = 30.mysp,
                )
            )
            Spacer(modifier = Modifier.height(30.px))
            Row(verticalAlignment = Alignment.CenterVertically) {
                Text(
                    maxLines = 1,
                    text = "适用房型",
                    style = TextStyle(
                        color = MYBLACK, fontSize = 24.mysp,
                    )
                )
                Spacer(modifier = Modifier.width(20.px))
                    Text(
                        style = TextStyle(
                            color = MYBLACK, fontSize = 24.mysp,
                        ),
                        text = commodity?.useTypeStr ?: "",
                        modifier = Modifier
                            .background(
                                color = MYBLACK_Percent5,
                                shape = RoundedCornerShape(6.px)
                            ) // Set gray background
                            .padding(
                                horizontal = 17.px,
                                vertical = 10.px
                            ), // Set horizontal padding
                    )
            }
            Spacer(modifier = Modifier.height(30.px))
            Row(verticalAlignment = Alignment.CenterVertically) {
                Text(
                    maxLines = 1,
                    text = "风       格",
                    style = TextStyle(
                        color = MYBLACK, fontSize = 24.mysp,
                    )
                )
                Spacer(modifier = Modifier.width(20.px))

                if (commodity != null) {
                    val style = commodity!!.styleName
                    Log.e(TAG,"风格:"+style)
                    style?.let {
                    it.split(",").forEach { name ->
                        Text(
                            style = TextStyle(
                                color = MYBLACK, fontSize = 24.mysp,
                            ),
                            text = name,
                            modifier = Modifier
                                .background(
                                    color = MYBLACK_Percent5,
                                    shape = RoundedCornerShape(6.px)
                                ) // Set gray background
                                .padding(
                                    horizontal = 17.px,
                                    vertical = 10.px
                                ), // Set horizontal padding
                        )
                        Spacer(modifier = Modifier.width(10.px))
                    }
                        }
                }
            }
            Spacer(modifier = Modifier.height(30.px))
            Row(verticalAlignment = Alignment.CenterVertically) {
                Text(
                    maxLines = 1,
                    text = "喜       好",
                    style = TextStyle(
                        color = MYBLACK, fontSize = 24.mysp,
                    )
                )
                Spacer(modifier = Modifier.width(20.px))
                commodity?.interestName?.let {
                    Text(
                        style = TextStyle(
                            color = MYBLACK, fontSize = 24.mysp,
                        ),
                        text = commodity?.interestName ?: "",
                        modifier = Modifier
                            .background(
                                color = MYBLACK_Percent5,
                                shape = RoundedCornerShape(6.px)
                            ) // Set gray background
                            .padding(
                                horizontal = 17.px,
                                vertical = 10.px
                            ), // Set horizontal padding
                    )
                }
            }
            Spacer(modifier = Modifier.height(30.px))
            Row(verticalAlignment = Alignment.CenterVertically) {
                Text(
                    maxLines = 1,
                    text = "标       签",
                    style = TextStyle(
                        color = MYBLACK, fontSize = 24.mysp,
                    )
                )

                Spacer(modifier = Modifier.width(20.px))

                if (commodity != null) {
                    val labelNames = commodity!!.labelName
                    labelNames?.split(",")?.forEach { name ->
                        Text(
                            style = TextStyle(
                                color = MYBLACK, fontSize = 24.mysp,
                            ),
                            text = name,
                            modifier = Modifier
                                .background(
                                    color = MYBLACK_Percent5,
                                    shape = RoundedCornerShape(6.px)
                                ) // Set gray background
                                .padding(
                                    horizontal = 17.px,
                                    vertical = 10.px
                                ), // Set horizontal padding
                        )
                        Spacer(modifier = Modifier.width(10.px))
                    }
                }
            }
            Spacer(modifier = Modifier.height(30.px))
            Row(verticalAlignment = Alignment.CenterVertically) {
                Text(
                    maxLines = 1,
                    text = "创  建  者",
                    style = TextStyle(
                        color = MYBLACK, fontSize = 24.mysp,
                    )
                )
                Spacer(modifier = Modifier.width(20.px))
                    Text(
                        style = TextStyle(
                            color = MYBLACK, fontSize = 24.mysp,
                        ),
                        text = if(commodity != null) { commodity!!.companyName + "/" +commodity!!.designerName } else "",
                        modifier = Modifier
                            .background(
                                color = MYBLACK_Percent5,
                                shape = RoundedCornerShape(6.px)
                            ) // Set gray background
                            .padding(
                                horizontal = 17.px,
                                vertical = 10.px
                            ), // Set horizontal padding
                    )
            }

            Spacer(modifier = Modifier.height(30.px))
            Row(verticalAlignment = Alignment.CenterVertically) {
                Text(
                    maxLines = 1,
                    text = ("渲染次数  " + commodity?.renderCount),
                    style = TextStyle(
                        color = Color.Black.copy(alpha = 0.6f), fontSize = 20.mysp,
                    )
                )
                Spacer(modifier = Modifier.width(20.px))
                Text(
                    maxLines = 1,
                    text = ("应用数量  " + commodity?.applyCount),
                    style = TextStyle(
                        color = Color.Black.copy(alpha = 0.6f), fontSize = 20.mysp,
                    )
                )
                Spacer(modifier = Modifier.width(12.px))
                Image(painter = painterResource(R.drawable.heart), contentDescription = "")
                Spacer(modifier = Modifier.width(8.px))
                Text(
                    maxLines = 1,
                    text = ""+commodity?.likeNum,
                    style = TextStyle(
                        color = Color.Black.copy(alpha = 0.6f), fontSize = 20.mysp,
                    )
                )
                Spacer(modifier = Modifier.width(16.px))
                Image(painter = painterResource(R.drawable.star), contentDescription = "")
                Spacer(modifier = Modifier.width(8.px))
                Text(
                    maxLines = 1,
                    text = ""+commodity?.collectNum,
                    style = TextStyle(
                        color = Color.Black.copy(alpha = 0.6f), fontSize = 20.mysp,
                    )
                )
                Spacer(modifier = Modifier.width(16.px))
                Image(painter = painterResource(R.drawable.browse), contentDescription = "")
                Spacer(modifier = Modifier.width(8.px))
                Text(
                    maxLines = 1,
                    text = ""+commodity?.watchNum,
                    style = TextStyle(
                        color = Color.Black.copy(alpha = 0.6f), fontSize = 20.mysp,
                    )
                )
            }
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        Log.e(TAG, "onDestroy")
    }

    companion object {
        val TAG = "商品详情"
    }
}
